package com.designoutput.ui.options.components
{
	import com.designoutput.enum.ToolType;
	import com.designoutput.icon.*;
	import com.designoutput.ui.controls.ToolButton;
	import com.designoutput.ui.options.BaseOption;
	
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import com.designoutput.manager.ResourceManager;
	
	/**
	 * A simple group to manage the current edited element's depth 
	 * 
	 */	
	public class DepthGroup extends Sprite
	{
		private var baseOption:BaseOption;
		
		public var bringToFrontTB:ToolButton;
		public var bringForwardTB:ToolButton;
		public var sendBackwardTB:ToolButton;
		public var sendToBackTB:ToolButton;
		
		public function DepthGroup(baseOption:BaseOption)
		{
			this.baseOption = baseOption;
			bringToFrontTB = new ToolButton(ToolType.BringToFront, new BringToFrontIcon(0,0), 5,5);
			bringToFrontTB.addEventListener(MouseEvent.MOUSE_DOWN, this.DepthBtnMouseDown);
			bringToFrontTB.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			bringToFrontTB.addEventListener(MouseEvent.MOUSE_OUT, this.ToolMouseOut);
			bringToFrontTB.x = 0;
			bringToFrontTB.y = 0;
			this.addChild(bringToFrontTB);
			
			bringForwardTB = new ToolButton(ToolType.BringForward, new BringForwardIcon(0,0), 5,5);
			bringForwardTB.addEventListener(MouseEvent.MOUSE_DOWN, this.DepthBtnMouseDown);
			bringForwardTB.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			bringForwardTB.addEventListener(MouseEvent.MOUSE_OUT, this.ToolMouseOut);
			bringForwardTB.x = 30;
			bringForwardTB.y = 0;
			this.addChild(bringForwardTB);
			
			sendBackwardTB = new ToolButton(ToolType.SendBackward, new SendBackwardIcon(0,0), 5,5);
			sendBackwardTB.addEventListener(MouseEvent.MOUSE_DOWN, this.DepthBtnMouseDown);
			sendBackwardTB.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			sendBackwardTB.addEventListener(MouseEvent.MOUSE_OUT, this.ToolMouseOut);
			sendBackwardTB.x = 60;
			sendBackwardTB.y = 0;
			this.addChild(sendBackwardTB);
			
			sendToBackTB = new ToolButton(ToolType.SendToBack, new SendToBackIcon(0,0), 5,5);
			sendToBackTB.addEventListener(MouseEvent.MOUSE_DOWN, this.DepthBtnMouseDown);
			sendToBackTB.addEventListener(MouseEvent.MOUSE_OVER, this.ToolMouseOver);
			sendToBackTB.addEventListener(MouseEvent.MOUSE_OUT, this.ToolMouseOut);
			sendToBackTB.x = 90;
			sendToBackTB.y = 0;
			this.addChild(sendToBackTB);
		}
		
		private function ToolMouseOver(evt:MouseEvent):void{
			var type:String = (evt.currentTarget as ToolButton).type;
			switch(type){
				case ToolType.BringToFront:
					this.baseOption.designOutputCard.hover.setText(ResourceManager.getValue("rm_bring_to_front"));
					break;
				case ToolType.BringForward:
					this.baseOption.designOutputCard.hover.setText(ResourceManager.getValue("rm_bring_forward"));
					break;
				case ToolType.SendBackward:
					this.baseOption.designOutputCard.hover.setText(ResourceManager.getValue("rm_send_backward"));
					break;
				case ToolType.SendToBack:
					this.baseOption.designOutputCard.hover.setText(ResourceManager.getValue("rm_send_to_back"));
					break;
				default:
					break;
			}
		}
		private function DepthBtnMouseDown(evt:MouseEvent):void{
			var toolBtn:ToolButton = evt.currentTarget as ToolButton;
			if(toolBtn.isMarked()){
				toolBtn.Mark(false);
			}
			toolBtn.addEventListener(MouseEvent.MOUSE_UP, DepthBtnMouseUp);
			this.baseOption.designOutputCard.command.changeElementDepth(toolBtn.type);
			toolBtn.Mark(true);
			
		}
		
		private function DepthBtnMouseUp(evt:MouseEvent):void{
			var toolBtn:ToolButton = evt.currentTarget as ToolButton;
			toolBtn.Mark(false);
		}
		
		private function ToolMouseOut(evt:MouseEvent):void{
			this.baseOption.designOutputCard.hover.hideEvent(evt);
			var toolBtn:ToolButton = evt.currentTarget as ToolButton;
			if(toolBtn.isMarked()){
				toolBtn.Mark(false);
			}
			
		}
		
	}
}